import store from "@/store"

const ctrlKey = 17,
	vKey = 86, // 粘贴
	cKey = 67, // 复制
	dKey = 68, // 删除
	xKey = 88, // 剪切

	yKey = 89, // 重做
	zKey = 90, // 撤销
	sKey = 83, // 保存
	deleteKey = 46 // 删除

// 与组件状态无关的操作
const globalmap = {
	[yKey]: recover,
	[zKey]: revock,
	[sKey]: save,
}

const editormap = {
	[vKey]: paste,
	[cKey]: copy,
	[dKey]: _delete,
	[xKey]: cut,
}

function globalKeydown(e) {
	if (e.ctrlKey) {
		let { keyCode } = e;
		if (globalmap[keyCode]) {
			e.preventDefault();
			globalmap[keyCode]()
		}
	}
}

export function listenGlobalKeydown() {
	document.addEventListener("keydown", globalKeydown)
}

export function removeGlobalKeydown() {
	document.removeEventListener("keydown", globalKeydown)

}

export function listenEditorKeydown(e) {
	if (e.ctrlKey) {
		let { keyCode } = e;
		if (editormap[keyCode]) {
			e.preventDefault();
			editormap[keyCode]()
		}

	}
}

function copy() {
	store.commit('copy')
}

function paste() {
	store.commit('paste')
}

function cut() {
	store.commit('cut')
}

function _delete() {
	store.commit('delete')
}

function revock() {
	store.commit('backP')
}

function recover() {
	store.commit('forwardP')
}

function save() {
	store.commit('saveData')
}
